Public Class M_Unit
    Dim dtable As New DataTable
    Dim dadapter As New OleDb.OleDbDataAdapter
    Private t0, t1, t2, t3, t4, t5, t6
    Private action As String

    Private Sub load_grid(ByVal sql As String)
        dtable.Clear()
        open_con()
        dadapter = New OleDb.OleDbDataAdapter(sql, DbCon)
        dadapter.Fill(dtable)
        DataGridView1.DataSource = dtable
        DataGridView1.Columns("unit_id").Visible = False
        DataGridView1.Columns("tenantid").Visible = False
        'DataGridView1.Columns(4).HeaderText = "Level"
        DataGridView1.Columns(4).DefaultCellStyle.Format = "##,###"
        DataGridView1.Columns(5).DefaultCellStyle.Format = "##,###"
        'DataGridView1.Columns(6).DefaultCellStyle.Format = "##,###"
        DataGridView1.RowHeadersVisible = False
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridView1.AutoResizeColumns()
        DataGridView1.ReadOnly = True
        DbCon.Close()
    End Sub
    Private Sub detail(ByVal id)
        open_db("select * from vw_unit where unit_id=" & id)
        While Read.Read()
            Me.TextBox1.Text = isnull(Read("no_kios"))
            Me.TextBox6.Text = isnull(Read("tenantid"))
            Me.TextBox2.Text = isnull(Read("nama"))
            If Read(4) Is DBNull.Value Then
                Me.TextBox3.Text = 0
            Else
                Me.TextBox3.Text = isnull(Format(Read("harga"), "##,###"))
            End If
            If Read(5) Is DBNull.Value Then
                Me.TextBox4.Text = 0
            Else
                Me.TextBox4.Text = isnull(Format(Read("ppn"), "##,###"))
            End If
            If Read(6) Is DBNull.Value Then
                Me.TextBox5.Text = 0
            Else
                Me.TextBox5.Text = isnull(Format(Read("total"), "##,###"))
            End If
            Me.RichTextBox1.Text = isnull(Read("keterangan"))
        End While
        close_db()
        set_param(id)
    End Sub
    Private Sub set_param(ByVal id)
        If id <> "" Then
            t0 = id
            t1 = TextBox1.Text
            t2 = TextBox2.Text
            t3 = TextBox3.Text
            t4 = TextBox4.Text
            t5 = TextBox5.Text
            t6 = TextBox6.Text
        Else
            t0 = id
            t1 = id
            t2 = id
            t3 = id
            t4 = id
            t5 = id
            t6 = id
        End If
    End Sub
    Private Sub save()
        Dim qry As String = ""
        Dim i As Integer
        If Not cek_null() Then
            If action = "NEW" Then
                qry = "insert into m_unit (no_kios,tenant_id,harga,ppn,total) values ('" & TextBox1.Text & "'," & TextBox6.Text & "," & TextBox3.Text & "," & TextBox4.Text & "," & TextBox5.Text & ")"             
            ElseIf action = "EDIT" Then
                qry = "update m_unit set "
                i = 0
                If TextBox1.Text <> t1 Then
                    qry = qry + "no_kios='" & TextBox1.Text & "'"
                    i += 1
                End If
                If TextBox2.Text <> t2 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "tenant_id=" & TextBox6.Text
                End If
                If TextBox3.Text <> t3 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "harga=" & TextBox3.Text
                End If
                If TextBox4.Text <> t4 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "ppn=" & TextBox4.Text
                End If
                If TextBox5.Text <> t5 Then
                    If i > 0 Then
                        qry = qry + ","
                    End If
                    i += 1
                    qry = qry + "total=" & TextBox5.Text
                End If
                qry = qry + " where id =" & t0
            End If
            exec_qr(qry)
            load_grid("select * from vw_unit")
        Else
            MsgBox("Harap mengisi semua data", MsgBoxStyle.Information, "Isi Data")
        End If
    End Sub

    Private Function cek_change() As Boolean
        If TextBox1.Text <> t1 Then
            Return True
        Else
            If TextBox2.Text <> t2 Then
                Return True
            Else
                If TextBox3.Text <> t3 Then
                    Return True
                Else
                    If TextBox4.Text <> t4 Then
                        Return True
                    Else
                        If TextBox5.Text <> t5 Then
                            Return True
                        Else
                            Return False
                        End If
                    End If
                End If
            End If
        End If
    End Function
    Private Function cek_null() As Boolean
        If TextBox1.Text = "" Then
            cek_null = True
        Else
            If TextBox2.Text = "" Then
                cek_null = True
            Else
                cek_null = False
            End If
        End If
    End Function

    Private Sub M_Unit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_grid("select * from vw_unit")
        'detail(0)
        action = ""
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        'Dim jwb
        'action = "EDIT"
        'If cek_change() Then
        '    jwb = MsgBox("Data telah berubah, apakah data akan di simpan?", MsgBoxStyle.YesNo, "Save Changes")
        '    If jwb = 6 Then
        '        save()
        '    End If
        'End If
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        action = "NEW"
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        set_param("")
        TextBox1.Focus()
        form_aktif(False)
        toolbar_aktif(False)
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        save()
        form_aktif(True)
        action = ""
        toolbar_aktif(True)
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim x
        Dim sql As String
        If action = "NEW" Or action = "EDIT" Then
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
            form_aktif(True)
            action = "CANCEL"
            toolbar_aktif(True)
        Else
            action = "DEL"
            x = MsgBox("Apakah yakin akan menghapus data?", MsgBoxStyle.YesNo, "Detele Data")
            If x = 6 Then
                sql = "delete from m_unit where unit_id=" & t0
                exec_qr(sql)
                load_grid("select * from vw_unit")
            End If
        End If
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, 0)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = 1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i - 1 >= 0 Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i - 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
        Dim i As Integer
        If DataGridView1.CurrentRow Is Nothing Then
            i = -1
        Else
            i = DataGridView1.CurrentRow.Index
        End If
        If i + 1 < DataGridView1.RowCount Then
            DataGridView1.CurrentCell = DataGridView1.Item(1, i + 1)
            detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
        End If
    End Sub

    Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
        DataGridView1.CurrentCell = DataGridView1.Item(1, DataGridView1.RowCount - 1)
        detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value)
    End Sub

    Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click
        Me.Close()
    End Sub

    Private Sub TextBox2_KeyDown1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Keys.F3 Then
            Dim frm2 As New P_Tenant
            SQL = "select * from m_tenant_id"
            namafrm = "Select Tenant"
            Me.TextBox2.Text = frm2.getselection
            TextBox6.Text = T_id
        End If
    End Sub

    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        Dim p
        If TextBox3.Text <> "" Then
            p = Val(TextBox3.Text.Replace(".", ""))
            If IsNumeric(TextBox3.Text) Then
                TextBox4.Text = Format(p * 0.1, "##,###")
                TextBox5.Text = Format(p * 1.1, "##,###")
                TextBox3.Text = Format(p, "##,###")
            Else
                MsgBox("Masukkan nominal !", MsgBoxStyle.Information, "Input Error")
                TextBox3.Text = ""
                TextBox3.Focus()
            End If
        End If
    End Sub
    Private Sub DataGridView1_Keyup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Down Then
            If DataGridView1.CurrentRow.Index + 1 < DataGridView1.RowCount Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index + 1).Value)
            End If
        End If
        If e.KeyCode = Keys.Up Then
            If DataGridView1.CurrentRow.Index - 1 >= 0 Then
                detail(DataGridView1.Item(0, DataGridView1.CurrentRow.Index - 1).Value)
            End If
        End If
    End Sub

    Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click
        If SearchText.Text.Length = 0 Then
            load_grid("select * from vw_unit")
        Else
            load_grid("select * from vw_unit where " & SearchMode.Text & " like '%" & SearchText.Text & "%'")
        End If
    End Sub

    Private Sub form_aktif(ByVal a As Boolean)
        If action = "NEW" Then
            TextBox1.ReadOnly = a
        End If
        TextBox2.ReadOnly = a
        TextBox3.ReadOnly = a
        TextBox4.ReadOnly = a
        TextBox5.ReadOnly = a
        RichTextBox1.ReadOnly = a
        DataGridView1.Enabled = a
    End Sub

    Private Sub toolbar_aktif(ByVal a As Boolean)
        ToolStripButton1.Enabled = a
        ToolStripButton10.Enabled = a
        ToolStripButton2.Enabled = Not a
        'ToolStripButton3.Enabled = Not a
        ToolStripButton4.Enabled = a
        ToolStripButton5.Enabled = a
        ToolStripButton6.Enabled = a
        ToolStripButton7.Enabled = a
        ToolStripButton8.Enabled = a
        SearchMode.Enabled = a
        SearchText.Enabled = a
        ToolStripButton9.Enabled = a
        If action = "NEW" Or action = "EDIT" Then
            ToolStripButton3.Text = "Cancel"
        Else
            ToolStripButton3.Text = "Delete"
        End If
    End Sub

    Private Sub ToolStripButton10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton10.Click
        action = "EDIT"
        form_aktif(False)
        toolbar_aktif(False)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2_KeyDown1(Me, EventArgs.Empty)
    End Sub
End Class